package com.ljh.mybatis.config;

import com.ljh.mybatis.entity.Department;
import java.util.ArrayList;
import java.util.List;

/**
 * @auth 刘佳浩
 * @create 2020-11-10-8:27
 */
public class Build {
    public static List<Department> build(List<Department> list){
        ArrayList<Department> trees = new ArrayList<>();
        for (Department listNode : list) {
            if (listNode.getPid()==0){
                listNode.setLevel(1);
                trees.add(findChildren(listNode,list));
            }
        }
        return trees;
    }

    public static Department findChildren(Department listNode, List<Department> list) {
        listNode.setChildren(new ArrayList<Department>());
        for (Department it : list) {
            if (listNode.getId().equals(it.getPid())){
                int level = listNode.getLevel() + 1;
                it.setLevel(level);
                if (listNode.getChildren()==null){
                    listNode.setChildren(new ArrayList<Department>());
                }
                listNode.getChildren().add(findChildren(it,list));
            }
        }
        return listNode;
    }
}
